
package com.be.beadmin.basicData.impl;

import com.be.beadmin.basicData.entity.Customer;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.basicData.entity.Customer_;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.basicData.mapper.CustomerMapperEx;
import com.be.beadmin.basicData.repository.CustomerRepository;
import com.be.beadmin.basicData.service.CustomerService;
import com.be.beadmin.basicData.dto.input.CustomerDto;
import com.be.beadmin.basicData.dto.output.CustomerEx;
import com.be.beadmin.basicData.dto.input.CustomerQueryCriteria;
import com.be.beadmin.basicData.mapper.CustomerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author chenwei
* @date 2022-12-23
**/
@Service
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private  CustomerRepository customerRepository;
    @Autowired
    private  CustomerMapper customerMapper;
    @Autowired
    private  CustomerMapperEx customerExMapper;

    @Override
    public Page<CustomerEx> getCustomerByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<Customer> page = customerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(Customer_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Customer_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<CustomerEx> page1 = page.map(customerExMapper::toEntity);
            return page1;

    }

    @Override
    public void addCustomer(CustomerDto customerDto) {
            List<Customer> customers = customerRepository
                    .findCustomerByCustomerCodeAndDelete(customerDto.getCustomerCode(), 0);
            if(ObjectUtil.isNotEmpty(customers) && customers.size()> 0) {
                throw new BeadminException("客户编码已经存在,请重新添加");
            }
            Customer customer = customerMapper.toEntity(customerDto);
            customerRepository.save(customer);
    }

    @Override
    public void updateCustomer(CustomerDto customerDto) {
        Customer result = customerRepository.findCustomerByIdAndDelete(customerDto.getId(), 0);
        if(ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("Customer不存在,请重新查询");
        }
        result = customerMapper.toEntity(customerDto);
        customerRepository.save(result);
    }


    @Override
    public void delCustomer(String id) {
        Customer customer = customerRepository.findCustomerById(id);
        customer.setDelete(1);
        customerRepository.save(customer);
    }

    @Override
    public void delCustomers (List<String> ids) { }
}